package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.IntegerConstant;
import com.itheima.reggie.common.NameConstant;
import com.itheima.reggie.common.ServerResultVO;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * dish
 * @author 武帅丞
 * @date 2024/09/23
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public ServerResultVO<String> add(@RequestBody DishDto dishDto) {
        log.info("add dish with flavor, dishDto:{}", dishDto);
        dishService.addWithFlavor(dishDto);
        return ServerResultVO.success("successfully add dish");
    }

    @PutMapping
    public ServerResultVO<String> update(@RequestBody DishDto dishDto) {
        log.info("update dish with flavor, dishDto:{}", dishDto);
        dishService.updateWithFlavor(dishDto);
        return ServerResultVO.success("successfully update dish");
    }

    @Transactional
    @GetMapping("/page")
    public ServerResultVO<Page> page(int page, int pageSize, String name) {
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Dish::getName, name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(pageInfo, queryWrapper);

        Page<DishDto> resultPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, resultPage, "records");
        resultPage.setRecords(pageInfo.getRecords().stream().map(item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Category category = categoryService.getById(item.getCategoryId());
            dishDto.setCategoryName((category != null) ? category.getName() : NameConstant.DEFAULT_CATEGORY_NAME);
            return dishDto;
        }).collect(Collectors.toList()));
        return ServerResultVO.success(resultPage);
    }

    @GetMapping("/{id}")
    public ServerResultVO<DishDto> get(@PathVariable Long id) {
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return ServerResultVO.success(dishDto);
    }

    @GetMapping("/list")
    public ServerResultVO<List<Dish>> list(Dish dish) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        queryWrapper.eq(Dish::getStatus, IntegerConstant.DISH_STATUS_VALID);
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> dishList = dishService.list(queryWrapper);
        return ServerResultVO.success(dishList);
    }
}
